<?php
class zona extends CI_Controller {
	var $titp='zona';
	var $url ='zona/';
	//var $reg ='mision/';

	function index(){
		redirect($this->url.'filteredgrid');
	}

	function filteredgrid(){
		$this->load->library('rapyd');

		$grid = new datagrid_library();

		$grid->label = 'Lista de las zonas ';
		$grid->per_page = 40;
		$grid->cid = '';
		$grid->source('zonas');

		$grid->column('nombre','Nombre',true)->url('zona/dataedit/show/{id}');
		$grid->column('caracteristicas','Caracteristicas',true);
		$grid->column('precipitacion','Cantidad de precipitacion',true);
		$grid->column('temperatura','Temperatura',true);
		//$link=anchor_popup('zona/plantas/{id}','Imprimir',array());
		//$grid->column($link ,'Imprimir' ,true)
			//->attributes(array('align'=>'right'));

		$grid->add_button(array('url'=>$this->url.'dataedit/create'));
		$action = "javascript:window.location.href='".site_url('/')."'";
		$grid->button('btn_reg', 'Regresar', $action, 'TR');
		$grid->build();

		$data['content'] = $grid->output;
		$data['head']    = $this->rapyd->head().script('jquery.js');
		$data['title']   = '';
		$this->load->view('view_ventanas_sola', $data);
	}

	function dataedit(){
		$this->load->library('rapyd');

		$edit = new dataedit_library();
		$edit->label = 'Lista de Zona';
		$edit->back_url = site_url($this->url.'filteredgrid');

		$edit->source('zonas');
		$edit->pre_process(array('delete'), array($this, 'pre_delete'));

		$edit->field('input','nombre','Nombre')
			->rule('trim|required|unique');

		$edit->field('textarea','caracteristicas','Características')
			->rule('trim|required');

		$edit->field('input','precipitacion','Precipitación')
			->rule('trim|required|numeric');

		$edit->field('input','temperatura','Temperatura')
			->rule('trim|required|numeric');

		$edit->buttons('modify','save','undo','delete','back');

		$edit->build();

		$data['head']    = $this->rapyd->head();
		$data['title']   = '<h1>Características de la zona</h1>';
		$data['content'] = $edit;
		$this->load->view('view_ventanas_sola', $data);
	}

	function pre_delete($model){
		$id = $model->get('id');

		$this->db->from('cuencas');
		$this->db->where('id_zona',$id);
		$cana = $this->db->count_all_results();

		if($cana==0){
			return true;
		}
		$model->error_string = '--No se puede eliminar una zona con cuencas relacionas.';
		return false;
	}

	function plantas($id){
		$this->load->helper('date');
		$this->db->select(array('a.*','d.nombre AS comite','d.razon AS razon','d.cuenta AS cuenta','e.nombre AS bnombre'));
		$this->db->from('proyectos AS a');
		$this->db->join('parroquias AS b','a.id_parroquia=b.id');
		$this->db->join('zonas AS c'     ,'a.id_zonas=c.id');
		$this->db->join('comite AS d'    ,'a.id_comite=d.id');
		$this->db->join('tban AS e','d.id_tban=e.id');
		
		$this->db->where('a.id',$id);
		$query = $this->db->get();

		$data = array();
		if ($query->num_rows() > 0){
			$row  = $query->row();
			$data['nombre'] = $row->nombre;
			$data['comite'] = $row->comite;
			$data['numero'] = $row->numero;
			$data['estatus']= $row->status;
			$data['razon']  = $row->razon;
			$data['monto']  = $row->monto;
			$data['cuenta'] = $row->cuenta;
			$data['bcomite']  = $row->bnombre; 
		}

		$this->load->view('formatos/nplantas_por_zonas.php', $data);
	}

}

